package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoContact;
import org.itboys.mobile.service.mongo.weixin.ZhongBaoWeiXinContactService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：江雄
 * 时间：2017年5月9日
 * 描述：众包_微信_通讯录_controller
 */
@Controller
@RequestMapping("/zhongbao/weixin/contact")
public class ZhongBaoWeiXinContactController extends BaseController{
    @Autowired
    private ZhongBaoWeiXinContactService contactService;



    /**
     * 通讯录首页
     * @param request
     * @param response
     */
    @RequestMapping(value="/index", method= RequestMethod.GET)
    @ApiOperation(value="员工、同事、客户首页")
    public String index(
            @ApiParam(required = true, name = "type", value = "1,员工，2同事，3客户") @RequestParam(value = "type", required = true) Integer type,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ){
        try{
            if(type==1){
                return "/zhongbao/weixin/contact/employee/index";
            }else if(type==2){
                return "/zhongbao/weixin/contact/classmate/index";
            }else if(type==3){
                return "/zhongbao/weixin/contact/customer/index";
            }

            return "";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/contact/index----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }

    @RequestMapping(value="/getData", method= RequestMethod.POST)
    @ApiOperation(value="我的通讯录_数据页")
    public String getData(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required = true, name = "type", value = "1,员工，2，同事，3客户") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order

    ){
        try{

            List<ZhongBaoContact> contacts = contactService.getContact(request,AccountIdHoldUtils.getSessionMember(request).getId(),type);
            model.addAttribute("contacts",contacts);
            if(type==1){
                return "/zhongbao/weixin/contact/employee/data";
            }else if(type==2){
                return "/zhongbao/weixin/contact/classmate/data";
            }else if(type==3){
                return "/zhongbao/weixin/contact/customer/data";
            }
            return "";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/contact//getData----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }
    @RequestMapping(value="/input", method= RequestMethod.GET)
    @ApiOperation(value="通讯录修改页")
    public String input(
            @ApiParam(required = true, name = "contactId", value = "通讯录ID。新增为0") @RequestParam(value = "contactId", required = true) Long contactId,
            @ApiParam(required = true, name = "type", value = "1,员工，2同事，3客户") @RequestParam(value = "type", required = true) Integer type,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ){
        try{
            ZhongBaoContact contact=null;
            if(LongUtil.isNotZreo(contactId)){
                contact=contactService.getById(contactId);
            }else{
                contact=new ZhongBaoContact();
                contact.setId(contactId);
                contact.setType(type);
            }
            model.addAttribute("contact",contact);
            return "/zhongbao/weixin/contact/input";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/contact/input----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }

    @RequestMapping(value="/save", method=RequestMethod.POST)
    @ApiOperation(value="新增通讯录")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=true, name="contactId", value="项目ID。")@RequestParam(value = "contactId", required = true)Long contactId,
             @ApiParam(required=true, name="nickName", value="名称")@RequestParam(value = "nickName", required = true)String nickName,
            @ApiParam(required=true, name="mobile", value="联系电话。")@RequestParam(value = "mobile", required = true)String mobile,
            @ApiParam(required=false, name="remark", value="项目ID。")@RequestParam(value = "remark", required = false)String remark,
            @ApiParam(required=true, name="type", value="1,员工，2同事，3客户")@RequestParam(value = "type", required = true)Integer type
    ){
        try {
            contactService.saveContact(contactId,AccountIdHoldUtils.getSessionMember(request).getId(),nickName,mobile,remark,type);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/zhongbao/weixin/contact/save--------"+e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

}
